package com.ziyun.erp.web.modules.promotion.controller;

import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.common.e.promotion.SendCouponTypeEnum;
import com.ziyun.erp.common.e.promotion.SendUserTypeEnum;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.promotion.entity.PromotionPolicyConfigEntity;
import com.ziyun.erp.modules.promotion.service.PromotionPolicyConfigService;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ziyun.erp.modules.promotion.entity.ConfigCouponUcenterEntity;
import com.ziyun.erp.modules.promotion.service.ConfigCouponUcenterService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 优惠券领卷表
 *
 * @author jiangli
 * @email 
 * @date 2018-09-20 09:45:15
 */
@RestController
@RequestMapping("promotion/configcouponucenter")
public class ConfigCouponUcenterController {
    @Autowired
    private ConfigCouponUcenterService configCouponUcenterService;
    @Autowired
    private PromotionPolicyConfigService promotionPolicyConfigService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("promotion:configcouponucenter:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = configCouponUcenterService.queryPage(params);
        List<PromotionPolicyConfigEntity> couponList = promotionPolicyConfigService.selectList(new EntityWrapper<PromotionPolicyConfigEntity>()
                .eq("policy_type", "coupon")
                .eq("disabled", 0).gt("end_date",new Date()));
        Map<String, List> mapEnum = new HashMap<>();
        mapEnum.put("couponList", couponList); //优惠券信息
        return R.ok().put("page", page).put("userData", mapEnum);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("promotion:configcouponucenter:info")
    public R info(@PathVariable("id") Integer id){
        ConfigCouponUcenterEntity configCouponUcenter = configCouponUcenterService.selectById(id);

        return R.ok().put("configCouponUcenter", configCouponUcenter);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("promotion:configcouponucenter:save")
    public R save(@RequestBody ConfigCouponUcenterEntity configCouponUcenter){
        PromotionPolicyConfigEntity en = promotionPolicyConfigService.selectById(configCouponUcenter.getCouponId());
        configCouponUcenter.setCouponTitel(en.getName());
        SysUserEntity curentUser = UserUtils.getCurentUser();
        configCouponUcenter.setOperation(curentUser.getUsername());
        configCouponUcenterService.insert(configCouponUcenter);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("promotion:configcouponucenter:update")
    public R update(@RequestBody ConfigCouponUcenterEntity configCouponUcenter){
        ValidatorUtils.validateEntity(configCouponUcenter,UpdateGroup.class);
        configCouponUcenterService.updateById(configCouponUcenter);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("promotion:configcouponucenter:delete")
    public R delete(@RequestBody Integer[] ids){
        configCouponUcenterService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 优惠券列表,支付券列表
     */
    @GetMapping("/getCouponList")
    public R getCouponList(){
        List<PromotionPolicyConfigEntity> couponList = promotionPolicyConfigService.selectList(new EntityWrapper<PromotionPolicyConfigEntity>()
                .eq("policy_type", "coupon")
                .eq("disabled", 0).gt("end_date",new Date()));
        List<PromotionPolicyConfigEntity> payList = promotionPolicyConfigService.selectList(new EntityWrapper<PromotionPolicyConfigEntity>()
                .eq("policy_type", "pay")
                .eq("disabled", 0));
        return R.ok().put("couponList",couponList).put("payList",payList).put("sendUserTypeEnum",JsonUtils.enumToList(SendUserTypeEnum.class))
                .put("sendCouponTypeEnum",JsonUtils.enumToList(SendCouponTypeEnum.class));
    }

}
